package com.study.mina;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description:
 * User: 27010
 * DateTime: 2018/5/13 10:53
 */
public class SimpleServerHandler extends IoHandlerAdapter{
    private AtomicInteger count = new AtomicInteger(0);
    @Override
    public void sessionCreated(IoSession session) throws Exception {
       System.out.println("sessionCreated "+session.getLocalAddress());
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("sessionCreated "+session.getLocalAddress());
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("sessionCreated "+session.getLocalAddress());
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
       //
        System.out.println(cause.getMessage());
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        System.out.println("messageReceived "+session.getLocalAddress()  +" msg: "+message.toString());
        session.write(count.incrementAndGet());
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
       // super.messageSent(session, message);
    }
}
